// @ts-ignore
import * as echarts from '@/ec-canvas/echarts'
import { useEffect, useRef, useState } from 'react'

export default (poption) => {
    // 图表数据
    // const [chatData, setChatData] = useState(option.series[0].data)
    const [option, setOption] = useState(poption)
    const chart = useRef<any>()

    // option.series[0].data = chatData

    function initChart(canvas, width, height, dpr) {
        chart.current = echarts.init(canvas, null, {
            width: width,
            height: height,
            // devicePixelRatio: dpr, // 像素
        })
        canvas.setChart(chart)

        chart?.current?.setOption(option)
        return chart
    }

    const ec = {
        onInit: initChart,
    }

    const updateChart = (data: any) => {
        let newOption = { ...option }
        newOption.series[0].data = data
        setOption(newOption)
        setTimeout(() => {
            console.log(option)
            chart.current.setOption(option)
        }, 300)
    }

    // useEffect(() => {
    //     setTimeout(() => {
    //         console.log(chatData)
    //         chart?.current?.setOption(option)
    //     }, 300)
    // }, [chatData])

    return [ec, updateChart as any]
}
